package com.weavernorth.store.service;

import com.weaver.general.Util;
import com.weavernorth.store.entity.storeClockInEntity;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import jxl.read.biff.Record;
import lombok.val;
import oracle.jdbc.internal.OracleTypes;
import org.apache.log4j.Logger;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.StaticObj;
import weaver.interfaces.datasource.DataSource;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

import static com.weavernorth.util.MyCommonUtils.getUseridByCode;
import static com.weavernorth.util.dateUtils.getYearMonthDay;
import static com.weavernorth.util.dateUtils.isValidDate;

/**
 * @author 王文彬
 * @date 2020/10/27 16:03
 * @description 描述：巡店计划门店打卡记录从v8取数
 */
public class storeClockInService {
    private Logger log = Logger4jUtil.getLoggerByName("/Timmer/storeClockInTimmer/");
    private String gh;
    private String begindate;
    private String endate;
    public String getGh() {
        return gh;
    }
    public void setGh(String gh) {
        this.gh = gh;
    }
    public String getBegindate() {
        return begindate;
    }
    public void setBegindate(String begindate) {
        this.begindate = begindate;
    }
    public String getEndate() {
        return endate;
    }
    public void setEndate(String endate) {
        this.endate = endate;
    }

    /***
     * 主程序
     */
    public  void execute(){
        log.info("V8获取巡店人员打卡记录：");
        log.info("Begin>>>>>>>>>>>>");
        try {
            log.info("接口参数：gh=" + gh + ";begindate=" + begindate + ";enddate=" + endate);
            String strGh = "";
            String strBeginDate="";
            String strEndate="";
            String strYearMonth="";
            if (gh==null){
                strGh="";
            }else{
                strGh=gh;
            }
            //获取当前日期的前一天
            String strYesterday= DateUtil.getYesterday();
            log.info("当前日期的前一天日期："+strYesterday);

            String[] strDateArry = strYesterday.split("-");
            Integer year = Integer.parseInt(strDateArry[0]);
            Integer monthInt = Integer.parseInt(strDateArry[1]);
            Integer day=Integer.parseInt(strDateArry[2]);
            if(begindate==null&&endate==null) {
                if (day > 5) {//当前日期在当月5号之后,只取当月数据
                    strYearMonth = strYesterday.substring(0, 7);
                } else if (day < 5) {
                    strBeginDate = (isValidDate(begindate, "yyyy-MM-dd HH:mm:ss") ? begindate : (dateUtils.getPreMonthFirstDate() + " 00:00:00"));
                    strEndate = (isValidDate(endate, "yyyy-MM-dd HH:mm:ss") ? endate : (strYesterday + " 23:59:59"));
                }
            }else{
                strBeginDate=isValidDate(begindate, "yyyy-MM-dd HH:mm:ss") ? begindate : (getYearMonthDay()+" 00:00:00");
                strEndate=isValidDate(endate, "yyyy-MM-dd HH:mm:ss") ? endate :(getYearMonthDay()+" 23:59:59");
            }
            Map<String, List<storeClockInEntity>> mplist = getData(strYearMonth,strGh, strBeginDate, strEndate,log);
            if(mplist!=null) {
                List<storeClockInEntity> entityList = storeClockInService.filterList(mplist,log);
                log.info("待插入或更新的打卡记录：" + entityList.toString());
                if (entityList.size() > 0) {
                    InsertOrUpdateSigin(entityList,log);
                }
            }
            if(!strGh.equals("")) {
                String strSqls = "SELECT * FROM (select a.* from uf_v8record a where a.dkdate>='" + strBeginDate + "' and a.dkdate<='" + strEndate + "' and a.gh='" + strGh + "' ORDER BY dksj desc) WHERE ROWNUM=1";
                log.info("查询单条最大打卡时间记录："+strSqls);
                RecordSet rs=new RecordSet();
                rs.execute(strSqls);
                while (rs.next()){
                    String strGhs=rs.getString("gh");
                    //人员id
                    String strUserid=getUseridByCode(strGhs);
                    //手机号
                    String strPhone = rs.getString("phone");
                    //打卡门店
                    String strDkmd = rs.getString("dkmd");
                    //打卡日期时间
                    String strDksj = rs.getString("dksj");
                    //打卡日期
                    String strDkDate = rs.getString("dkdate");
                    //打卡时间
                    String strDkTime = rs.getString("dktime");
                    //打卡IP
                    String strDkIp =  rs.getString("dkip");
                    //打卡机器MAC地址
                    String strMac = rs.getString("macaddress");
                    //门店地址
                    String strMdAddress =rs.getString("mdaddress");
                    //打卡类型
                    String strSignTYpe="";
                    String dateFlag=strDkDate+" 12:00:00";
                    Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateFlag);
                    Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDksj);
                    if (date1.compareTo(date2)==-1) {//打卡时间早于12点前记录为上班
                        strSignTYpe="1";
                    }else{
                        strSignTYpe="2";
                    }
                    if(strSignTYpe.equals("2")) {//只更新下班打卡记录
                        log.info("更新打卡记录,员工id=" + strUserid + ";日期=" + strDkDate + ";打卡时间=" + strDksj);
                        updateData(strGh, strUserid, strDkDate, strDkTime, strDkIp, strMac, strMdAddress, strSignTYpe,log);
                    }
                }
            }
        }catch (Exception ex){
            log.info("接口异常,异常信息："+ex.toString());
        }
        log.info("End>>>>>>>>>>>>");
    }
    /***
     * SELECT * FROM hrmschedulesign WHERE ROWNUM<5
     * --1.找出某个员工某个日期最小打卡时间和最大打卡时间
     * --2.将最小打卡时间视为上班时间，最大打卡时间视为下班时间，分别判断是否存在，存在则更新不存在则插入
     * 从打卡建模表获取门店打卡数据
     * @param strWorkCode 员工号
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public static Map<String, List<storeClockInEntity>> getData(String strYearMonth,String strWorkCode, String beginDate, String endDate,Logger log) throws SQLException {
        List<storeClockInEntity> list = new ArrayList<>();

        log.info("参数变更前,工号：" + strWorkCode + ";开始时间：" + beginDate + ";结束时间：" + endDate+ ";本周期：" + strYearMonth);
        //对集合进行分组
        Map<String, List<storeClockInEntity>> mapList =null;
        try {
            String strGetAll="";
            if(!"".equals(strYearMonth)){//只取当月数据
                log.info("只取当月数据！");
                strGetAll="select a.* from uf_v8record a where SUBSTR(a.dkdate, 0, 7)='"+strYearMonth+"'";
            }else{
                log.info("取本月及上个月数据！");
                Date begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginDate);
                Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
                if(begin.compareTo(end)==1){//如果开始日期大于结束日期,则重新赋值结束日期;
                    endDate=beginDate.substring(0,10)+" 23:59:59";
                }
                log.info("参数转换：gh=" + strWorkCode + ";begindate=" + beginDate + ";enddate=" + endDate);
                strGetAll="select a.* from uf_v8record a where a.dkdate>='"+beginDate+"' and a.dkdate<='"+endDate+"'";
            }
            if(!"".equals(strWorkCode)){
                strGetAll +=" and a.gh='"+strWorkCode+"'";
            }
            strGetAll +=" and EXISTS(SELECT 1 FROM uf_xdjhb b WHERE b.sfkq is null AND a.dkdate=b.xdrq AND a.gh=b.ygh)";
            //获取信息结果集
            RecordSet rs = new RecordSet();
            log.info("查询所有未取得打卡记录sql："+strGetAll);
            rs.execute(strGetAll);
            log.info("参数变更后,工号：" + strWorkCode + ";开始时间：" + beginDate + ";结束时间：" + endDate+ ";本周期：" + strYearMonth);
            int rownum=0;
            //循环结果集
            while (rs.next()) {
                storeClockInEntity entity = new storeClockInEntity();
                //微信id
                String strVxId = Util.null2String(rs.getString("vxid"));
                log.info("微信id:"+strVxId);
                //工号
                String strGh = Util.null2String(rs.getString("gh"));
                log.info("工号:"+strGh);
                //手机号
                String strPhone = Util.null2String(rs.getString("phone"));
                log.info("手机号:"+strPhone);
                //打卡门店
                String strDkmd = Util.null2String(rs.getString("dkmd"));
                log.info("打卡门店:"+strDkmd);
                //打卡时间
                String strDksj = Util.null2String(rs.getString("dksj"));
                log.info("打卡日期时间:"+strDksj);
                String strDkdate = Util.null2String(rs.getString("dkdate"));
                log.info("打卡日期："+strDkdate);
                String strDktime = Util.null2String(rs.getString("dktime"));
                log.info("打卡时间："+strDktime);
                //打卡IP
                String strDkIp = Util.null2String(rs.getString("dkip"));
                log.info("打卡IP:"+strDkIp);
                //打卡机器MAC地址
                String strMac = Util.null2String(rs.getString("macaddress"));
                log.info("打卡机器MAC地址:"+strMac);
                //门店地址
                String strMdAddress = Util.null2String(rs.getString("mdaddress"));
                log.info("门店地址:"+strMdAddress);

                entity.setVxId(strVxId);
                entity.setGh(strGh);
                entity.setPhone(strPhone);
                entity.setDkmd(strDkmd);
                entity.setDksj(strDksj);
                if(strDksj.length()>9){
                    entity.setDkdate(strDkdate);
                    entity.setDkTime(strDktime);
                    entity.setGroupFild(strGh+"|"+strDkdate);//工号+打卡日期作为排序字段
                }else{
                    entity.setDkdate(strDksj);
                    entity.setDkTime(strDktime);
                }
                entity.setDkip(strDkIp);
                entity.setMackAddress(strMac);
                entity.setMdAddress(strMdAddress);
                log.info("实体类赋值");
                if(entity!=null){
                    list.add(entity);
                    log.info("打卡数据：" + entity.toString());
                }
            }
            log.info("list集合大小："+list.size());
            if (list.size()>0) {
                log.info("对集合进行分组");
                mapList = list.stream().collect(Collectors.groupingBy(storeClockInEntity::getGroupFild));
                log.info("分组后的数据:"+mapList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("填装数据异常>>>>>>>>" + e.toString());
        } finally {
            return mapList;
        }
    }

    /***
     * 筛选数据,返回要插入的相关打卡记录
     * @param mplist
     * @return
     */
    public static List<storeClockInEntity> filterList(Map<String, List<storeClockInEntity>> mplist,Logger log) {
        //Comparator.comparing(storeClockInEntity::getGroupFild).reversed()//降序
        log.info("过滤list中的打卡记录====>");
        List<storeClockInEntity> filerList=new ArrayList<>();
        try {
            log.info("开始筛选数据==>");
            if (mplist.size() > 0) {
                for (String key : mplist.keySet()) {
                    if (key.contains("|")&&key.length()>18) {//包含"|"才是正确数据,key由"工号|日期"组成
                        log.info("筛选能正确分组的数据：");
                        //1.获取员工某天的打卡记录并进行升序排序
                        List<storeClockInEntity> storeClockInList = mplist.get(key).stream().sorted(Comparator.comparing(storeClockInEntity::getDksj)).collect(Collectors.toList());
                        String strMds="";
                        log.info("获取打卡记录中的门店");
                        if(storeClockInList.size()>1) {//只取有两次打卡记录的人员
                            for (storeClockInEntity entity : storeClockInList) {
                                if (strMds.indexOf(entity.getDkmd()) == -1) {
                                    if (!"".equals(entity.getDkmd())) {
                                        strMds += entity.getDkmd() + ",";
                                    }
                                }
                            }
                        }
                        //2.获取工号
                        String strGh = key.substring(0, key.indexOf("|"));
                        //3.获取日期
                        String strDate = key.substring(key.indexOf("|") + 1, key.length());
                        log.info("员工："+strGh+",在日期："+strDate+"的打卡门店："+strMds);
                        storeClockInEntity signIn = null;//定义上班打卡对象
                        storeClockInEntity signOut = null;//下班打卡对象
                        //4.获取员工某天所有打卡记录对应的门店与巡店计划中打卡门店是否一致
                        Boolean mdIsExists=mdIsExists(strGh,strDate,strMds,log);
                        log.info("员工："+strGh+",在日期："+strDate+"的打卡记录对应的门店与巡店计划中打卡门店是否一致："+mdIsExists);
                        if(mdIsExists) {
                            //5.根据员工巡店计划判断上午/下午/全天对应的打卡记录
                            int iType = getDkType(strGh, strDate,log);
                            log.info("员工编号：" + strGh + ";日期：" + strDate + ";巡店计划类型：" + iType);
                            switch (iType) {
                                case 0:
                                    //只进行上班打卡数据操作,取第一条数据
                                    log.info("巡店计划-上班：");
                                    signIn = (storeClockInEntity) storeClockInList.get(0).clone();
                                    break;
                                case 1:
                                    //只进行下班打卡数据操作,取最后一条数据
                                    log.info("巡店计划-下班：");
                                    signOut = (storeClockInEntity) storeClockInList.get(storeClockInList.size() - 1).clone();
                                    break;
                                case 2:
                                    //全天巡店,取最小打卡时间作为上班打卡时间，取最晚打卡时间作为下班打卡时间
                                    //如果只存在一条记录,则只插入一条记录
                                    log.info("巡店计划-全天：");
                                    //当只有一条打卡记录时，比较时间
                                    if(storeClockInList.size()==1) {
                                        log.info("巡店计划为全天,但只有一条打卡记录,打卡时间为："+storeClockInList.get(0));
                                        String dateFlag=strDate+" 12:00:00";
                                        Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateFlag);
                                        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(storeClockInList.get(0).getDksj());
                                        if (date1.compareTo(date2)==-1) {//打卡时间早于12点前记录为上班
                                            signIn = (storeClockInEntity) storeClockInList.get(0).clone();

                                        }else{
                                            signOut = (storeClockInEntity) storeClockInList.get(storeClockInList.size() - 1).clone();
                                        }
                                    }else{
                                        String dateFlag=strDate+" 12:00:00";
                                        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateFlag);
                                        Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(storeClockInList.get(0).getDksj());
                                        Date date3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(storeClockInList.get(storeClockInList.size() - 1).getDksj());
                                        if(date2.compareTo(date1)==-1){//打卡时间早于12点前
                                            signIn = (storeClockInEntity) storeClockInList.get(0).clone();
                                        }
                                        if(date3.compareTo(date1)>-1){//打卡时间晚于12点
                                            signOut = (storeClockInEntity) storeClockInList.get(storeClockInList.size() - 1).clone();
                                        }
//                                        signIn = (storeClockInEntity) storeClockInList.get(0).clone();
//                                        signOut = (storeClockInEntity) storeClockInList.get(storeClockInList.size() - 1).clone();
                                    }
                                    break;
                                case 3:
                                    //无巡店计划,不进行打卡数据操作
                                    log.info("无巡店计划");
                                    break;
                            }
                        }
                        //5.将打卡数据插入到list集合
                        if (signIn != null) {
                            log.info("将上班打卡记录插入到list集合");
                            signIn.setDkType("1");//上班打卡
                            filerList.add(signIn);
                        }
                        if (signOut != null) {
                            log.info("将下班打卡记录插入到list集合");
                            signOut.setDkType("2");//下班打卡
                            filerList.add(signOut);
                        }

                    }
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("转换打卡数据出现异常,异常信息："+ex.toString());
        }finally {
            return  filerList;
        }
    }

    /***
     * 更新或插入打卡记录
     * @param list
     */
    public static void InsertOrUpdateSigin(List<storeClockInEntity> list,Logger log){
        log.info("执行插入/更新打卡数据的操作：");
        if (list.size()>0){
            for (storeClockInEntity entity : list) {
                //工号
                String strGh=entity.getGh();
                //人员id
                String strUserid=getUseridByCode(strGh);
                //手机号
                String strPhone = entity.getPhone();
                //打卡门店
                String strDkmd = entity.getDkmd();
                //打卡日期时间
                String strDksj = entity.getDksj();
                //打卡日期
                String strDkDate = entity.getDkdate();
                //打卡时间
                String strDkTime = entity.getDkTime();
                //打卡IP
                String strDkIp = entity.getDkip();
                //打卡机器MAC地址
                String strMac = entity.getMackAddress();
                //门店地址
                String strMdAddress =entity.getMdAddress();
                //打卡类型
                String strSignTYpe =entity.getDkType();
                if("".equals(strUserid)){
                    log.info("工号为"+strGh+"的人员在系统中不存在！");
                }else {
                        Boolean isExists = signIsExists(strUserid, strDkDate, strSignTYpe,log);
                        if (isExists) {//更新
                            log.info("更新打卡记录,员工id="+strUserid+";日期="+strDkDate);
                            Boolean isUpdate=isUpdate(strUserid, strDkDate, strSignTYpe,strDksj,log);
                            if(isUpdate){
                                updateData(strGh,strUserid, strDkDate, strDkTime, strDkIp, strMac, strMdAddress, strSignTYpe,log);
                            }
                        } else {//插入
                            log.info("插入打卡记录,员工id="+strUserid+";日期="+strDkDate);
                            insertData(strGh,strUserid, strDkDate, strDkTime, strDkIp, strMac, strMdAddress, strSignTYpe,log);

                        }
                }
            }
        }else{
            log.info("无可插入的数据！");
        }

    }

    /***
     * 查询打卡门店在巡店计划中是否存在
     * @param strGh
     * @param strDkDate
     * @param strDkmd
     * @return
     */
    private static Boolean mdIsExists(String strGh, String strDkDate, String strDkmd,Logger log) {
        Boolean result=true;
        RecordSet rs=new RecordSet();
        //巡店计划表
        String strQuerySql="SELECT * FROM uf_xdjhb WHERE ygh='"+strGh+"' AND xdrq='"+strDkDate+"'";
        String strDpSapId="";
        try {
                if (strDkmd.indexOf(",")>-1) {
                    strDkmd=strDkmd.substring(0,strDkmd.length()-1);
                }
                rs.execute(strQuerySql);
                while(rs.next()){
                    //巡店计划中的店铺编码
                    strDpSapId = Util.null2String(rs.getString("dp"));
                }
                //实际打卡的店铺
                String[] mdArry1 = strDkmd.split(",");
                //巡店计划的店铺
                String[] mdArry2 = strDpSapId.split(",");
                if(mdArry1.length>mdArry2.length){//实际打卡的店铺多于计划的店铺时
                   for(int i=0;i<mdArry2.length;i++){
                       if (strDkmd.indexOf(mdArry2[i])==-1){
                           log.info("员工："+strGh+"在"+strDkDate+"的打卡记录中的的店铺："+strDkmd);
                           log.info("员工："+strGh+"在"+strDkDate+"的巡店计划的店铺："+mdArry2[i]+"未打卡;不计入打卡表中!");
                           result=false;
                           break;
                       }
                   }
                }else if (mdArry1.length<mdArry2.length){//实际打卡的店铺少于计划的店铺时
                    log.info("员工："+strGh+"在"+strDkDate+"的巡店计划的店铺："+strDpSapId);
                    log.info("员工："+strGh+"在"+strDkDate+"的打卡记录中的的店铺："+strDkmd);
                    log.info("打卡门店与巡店计划门店不匹配,不计入打卡表中！");
                    result=false;
                }else{
                    String[] newArray=getArrayDif(mdArry1,mdArry2);
                    if(newArray.length>0){
                        log.info("员工："+strGh+"在"+strDkDate+"的巡店计划的店铺："+strDpSapId);
                        log.info("员工："+strGh+"在"+strDkDate+"的打卡记录中的的店铺："+strDkmd);
                        log.info("打卡门店与巡店计划门店不匹配,不计入打卡表中！");
                        result=false;
                    }else{
                        result=true;
                    }
                }

        }catch (Exception ex){
            ex.printStackTrace();
            log.info("查询打卡门店出现异常,异常信息："+ex.toString());
        }finally {
            return  result;
        }
    }

    /***
     * 更新打卡记录
     * @param strUserid
     * @param strDkDate
     * @param strDkTime
     * @param strDkIp
     * @param strMac
     * @param strMdAddress
     * @param strSignTYpe
     */
    private static void updateData(String strGh,String strUserid, String strDkDate, String strDkTime, String strDkIp, String strMac, String strMdAddress, String strSignTYpe,Logger log) {
        RecordSetTrans updateTrans = new RecordSetTrans();
        String strUpdate="update hrmschedulesign set isincom=1,signtime='"+strDkTime+"',clientaddress='"+strDkIp+"',signfrom='V8',addr='"+strMdAddress+"' where userid='"+strUserid+"' "+
                " and signdate='"+strDkDate+"' and signtype='"+strSignTYpe+"' ";
        try{
            updateTrans.setAutoCommit(false);
            updateTrans.executeSql(strUpdate);
            updateTrans.commit();
            log.info("打卡数据更新成功！");
            String strUpSql="update uf_xdjhb set sfkq='Y' where xdrq='"+strDkDate+"' and ygh='"+strGh+"'";
            log.info("更新是否已考勤字段sql:"+strUpSql);
            RecordSet rsUpdate=new RecordSet();
            rsUpdate.execute(strUpdate);
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("更新打卡记录异常,异常信息："+ex.toString());
            log.info("更新打卡记录sql:"+strUpdate);
            updateTrans.rollback();
        }
    }

    /***
     * 新增打卡数据
     * @param strUserid
     * @param strDkDate
     * @param strDkTime
     * @param strDkIp
     * @param strMac
     * @param strMdAddress
     * @param strSignTYpe
     */
    private static void insertData(String strGh,String strUserid, String strDkDate, String strDkTime, String strDkIp, String strMac, String strMdAddress, String strSignTYpe,Logger log) {
        RecordSetTrans addTrans = new RecordSetTrans();
//        if(!"1".equals(strSignTYpe)){
//            strDkTime="09:00:00";//上午或全天时默认9点
//        }
        String strInsert="INSERT INTO hrmschedulesign (userid,usertype,signtype,signdate,signtime,clientaddress,signfrom,addr,belongdate,isincom)VALUES"+
                "("+strUserid+",1,"+strSignTYpe+",'"+strDkDate+"','"+strDkTime+"','"+strDkIp+"','V8','"+strMdAddress+"','"+strDkDate+"',1)";
        log.info("插入打卡记录："+strInsert);
       try{
           addTrans.setAutoCommit(false);
           addTrans.executeSql(strInsert);
           addTrans.commit();
           String strUpdate="update uf_xdjhb set sfkq='Y' where xdrq='"+strDkDate+"' and ygh='"+strGh+"'";
           log.info("更新是否已考勤字段sql:"+strUpdate);
           RecordSet rsUpdate=new RecordSet();
           rsUpdate.execute(strUpdate);
           log.info("打卡数据插入成功！");
       }catch (Exception ex){
           ex.printStackTrace();
           log.info("插入打卡记录异常,异常信息："+ex.toString());
           log.info("插入打卡记录sql:"+strInsert);
           addTrans.rollback();
       }
    }

    /***
     * 查询打卡记录是否存在
     * @param strUserid 人员id
     * @param strDkDate 打卡日期
     * @param strSignTYpe 打卡类型：1,上班；2,下班
     * @return
     */
    private static Boolean isUpdate(String strUserid, String strDkDate, String strSignTYpe,String strDksj,Logger log) {
        Boolean result=false;
        try {
                Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDksj);
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String dateFormat1=sdf1.format(date1);
                RecordSet rs = new RecordSet();
                String strSql = "select  SIGNDATE,SIGNTIME from hrmschedulesign where userid='" + strUserid + "' and signtype='" + strSignTYpe + "' and signdate='" + strDkDate + "'";
                log.info("人员打卡记录是否存在sql语句：" + strSql);
                rs.execute(strSql);
                while (rs.next()) {
                    String date=Util.null2String(rs.getString("SIGNDATE"));
                    String time=Util.null2String(rs.getString("SIGNTIME"));
                    //打卡表中的时间
                    Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date+" "+time);
                    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                    String dateFormat2=sdf2.format(date1);
                    if("1".equals(strSignTYpe)){//上班打卡
                        if (date2.compareTo(date1)==-1){
                            //如果打卡表中的打卡时间小于当前巡店打卡记录中的时间时,不进行更新
                            log.info("上班打卡---人员id:"+strUserid+"的巡店打卡时间为："+dateFormat1+"晚于打卡表中的时间："+dateFormat2+",不进行更新数据操作！");
                            result = false;
                        }else{
                            log.info("上班打卡---人员id:"+strUserid+"的巡店打卡时间为："+dateFormat1+"早于打卡表中的时间："+dateFormat2+",进行更新数据操作！");
                            result=true;
                        }
                    }else{//下班打卡
                        if (date1.compareTo(date2)==-1){
                            //如果打卡表中的打卡时间大于当前巡店打卡记录中的时间时,不进行更新
                            log.info("上班打卡---人员id:"+strUserid+"的巡店打卡时间为："+dateFormat1+"早于打卡表中的时间："+dateFormat2+",不进行更新数据！");
                            result = false;
                        }else{
                            log.info("上班打卡---人员id:"+strUserid+"的巡店打卡时间为："+dateFormat1+"晚于打卡表中的时间："+dateFormat2+",进行更新数据！");
                            result=true;
                        }
                    }
                }
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("获取人员打卡记录发生异常,异常信息："+ex.toString());
        }finally {
            return result;
        }
    }

    /***
     *  判断打卡记录是否存在，存在则进行更新否则插入
     * @param strUserid
     * @param strDkdate
     * @param strSignType
     * @param log
     * @return
     */
    private static Boolean signIsExists(String strUserid,String strDkdate,String strSignType,Logger log){
        Boolean result=false;
        RecordSet rs=new RecordSet();
        try{
            String strSql = "select  SIGNDATE,SIGNTIME from hrmschedulesign where userid='" + strUserid + "' and signtype='" + strSignType + "' and signdate='" + strDkdate + "'";
            log.info("人员打卡记录是否存在sql语句：" + strSql);
            rs.execute(strSql);
            while (rs.next()){
                result=true;
            }
        }catch (Exception ex){
            log.info("查询人员"+strUserid+"在日期"+strDkdate+"的打卡记录时出现异常,异常信息："+ex.toString());
        }
        return result;
    }
    /***
     * 根据日期工号查询员工的巡店计划中巡店类型,如果不存在则返回0
     * @param strGh：员工号
     * @param strDate：日期
     * @return 巡店类型：上午、下午、全天
     */
    private static int getDkType(String strGh, String strDate,Logger log) {
        //todo
        RecordSet rs=new RecordSet();
        String strQuery="SELECT * FROM uf_xdjhb WHERE ygh='"+strGh+"' AND xdrq='"+strDate+"' and sfkq is NULL or sfkq='N'";
        rs.execute(strQuery);
        log.info("查找巡店计划："+strQuery);
        int result=3;
        String xdType="";
        while (rs.next()){
            xdType=rs.getString("xdsj");
        }
        if(xdType.equals("0")){//上午
            result=0;
        }else if(xdType.equals("1")){//下午
            result=1;
        }else if(xdType.equals("2")){//全天
            result=2;
        }
        return  result;
    }

    /**
     * 获取数组差集
     *
     * @param m
     * @param n
     * @return
     */
    private static String[] getArrayDif(String[] m, String[] n)
    {
        // 将较长的数组转换为set
        Set<String> set = new HashSet<String>(Arrays.asList(m.length > n.length ? m : n));

        // 遍历较短的数组，实现最少循环
        for (String i : m.length > n.length ? n : m)
        {
            // 如果集合里有相同的就删掉，如果没有就将值添加到集合
            if (set.contains(i))
            {
                set.remove(i);
            } else
            {
                set.add(i);
            }
        }

        String[] arr = {};
        return set.toArray(arr);
    }
}
